<?php

namespace App\Models;

use App\Service\Tree;

class SiteCate extends Base
{

    // 专栏作家
    protected $table = 'site_cate';

    // 缓存字段，用于搜索的字段
    protected $cacheFields = ['id',];

    // 获取所有的话题信息
    public function getAll($where = [], $order = 'rank ASC')
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = compact('where');
        if($is_cache){
            $cacheName = $this->cacheKey('lists', $args);
            $cacheData = $this->cacheClass()->getCache($cacheName);
            if(!empty($cacheData)){
                /*// 检查是否已经绑定
                if(($cacheData) && (!empty($cacheData['list']))){
                    $this->checkBangList($cacheData['list'], $cacheName);
                }*/

                return $cacheData;
            }
        }

        // 查询
        if(is_array($order)){
        } else{
            $order = explode(" ", $order);
        }

        $lists = $this->where($where)->orderby($order[0], $order[1])->get()->toArray();
        $lists = array_column($lists, null, 'id');
        // 保存为缓存
        if($is_cache){
            $this->addCacheList($lists, $cacheName, $lists, 'list');
        }

        return $lists;
    }

    // 匹配关键词到栏目
    public function keywords2Cate($content, $is_multiple = false)
    {
        $cates = $this->isCache(true)->getAll();
        $cates = is_object($cates) ? $cates->toArray() : $cates;

        // 整理
        $inCate = $cateIds = [];  // 已经有了的栏目（避免一篇文章多次匹配同一个栏目）
        foreach($cates as $_cate){
            $keywords = explode(',', $_cate['keyword']);
            foreach($keywords as $_keyword){
                $_cateId = $_cate['id'];
                if(empty($_keyword)){
                    continue;
                }
                // 如果这个栏目以及匹配了  则不在匹配这个栏目
                if(in_array($_cateId, $inCate)){
                    continue;
                }
                if(strpos($content, $_keyword) !== false){
                    // 一篇文章多个栏目
                    if($is_multiple){
                        $cateIds[] = $_cateId;
                    } else{
                        // 一篇文章一个栏目
                        if(empty($inCate)){
                            $cateIds[] = $_cateId;
                        }
                    }
                    $inCate[] = $_cateId;
                }
                // 如果没有分配到栏目，则分配到宏观里面
                if(empty($cateIds)){
                    $cateIds[] = 2;
                }
            }
        }

        // 组合数据的栏目

        return $cateIds;
    }


    // 获取某个栏目的下级id（用于获取文章）（不包含自己）
    public function getChildIds($id)
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = compact('id');
        if($is_cache){
            $cacheName = $this->cacheKey('getChildIds_ids', $args);
            $cacheData = $this->cacheClass()->getCache($cacheName);
            if(!empty($cacheData)){
                // 检查是否已经绑定
                if(($cacheData) && (!empty($cacheData))){
                    $this->checkBangList($cacheData, $cacheName);
                }

                return $cacheData;
            }
        }

        // 执行操作
        $lists = $this->isCache(true)->getAll();
        $tree  = new Tree();
        $ids   = $tree->getAllChildId($lists, $id);

        // 保存为缓存
        if($is_cache){
            $this->addCacheList($lists, $cacheName, $ids, 'list');
        }

        return $ids;
    }

    // 获取最顶层的父级信息
    public function getTop($id)
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = compact('id');
        if($is_cache){
            $cacheName = $this->cacheKey('getTop_is', $args);
            $cacheData = $this->cacheClass()->getCache($cacheName);
            if(!empty($cacheData)){
                /*// 检查是否已经绑定
                if(($cacheData) && (!empty($cacheData['list']))){
                    $this->checkBangList($cacheData['list'], $cacheName);
                }*/

                return $cacheData;
            }
        }

        // 执行操作
        $lists = $this->isCache(true)->getAll();
        $tree  = new Tree();
        $res   = $tree->getTopParent($id, $lists);

        // 保存为缓存
        if($is_cache){
            $this->addCacheList($lists, $cacheName, $res, 'list');
        }

        return $res;
    }

    // 获取父级信息
    public function getParent($id)
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = compact('id');
        if($is_cache){
            $cacheName = $this->cacheKey('getParent_is', $args);
            $cacheData = $this->cacheClass()->getCache($cacheName);
            if(!empty($cacheData)){
                /*// 检查是否已经绑定
                if(($cacheData) && (!empty($cacheData['list']))){
                    $this->checkBangList($cacheData['list'], $cacheName);
                }*/

                return $cacheData;
            }
        }

        // 执行操作
        $lists = $this->isCache(true)->getAll();
        $tree  = new Tree();
        $res   = $tree->getParent($id, $lists);

        // 保存为缓存
        if($is_cache){
            $this->addCacheList($lists, $cacheName, $res, 'list');
        }

        return $res;
    }

    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            'pid'    => 'numeric',
            'title'  => 'required',
            'rank'   => 'numeric',
            'status' => ['in:0,1,-1'],
        ];

        $msg = [
            'id'          => '编号',
            'rank'        => '排序',
            'title'       => '栏目名称',
            'pid'         => '父级id',
            'status'      => '状态（1-是正常）',
            'uid'         => '绑定的用户',
            'keyword'     => '关键字',
            'description' => '描述',
            'ctime'       => '创建时间',
            'mtime'       => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
